package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author huangqicai
 * @since 2022-10-21
 */
@Api(description = "课程章节里面的小结")
@RestController
@RequestMapping("/eduservice/video")
//原来需要在Controller上面加上@CrossOrigin解决跨域问题，现在gateway已经处理了   /*解决跨域问题*/
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    /*根据小节id查询小节信息*/
    @ApiOperation(value = "根据小节id查询小节信息")
    @GetMapping("/getVideoInfo/{id}")
    public R getVideoInfo(@PathVariable String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        return R.ok().data("eduVideo",eduVideo);
    }

    @ApiOperation(value = "添加小节信息")
    @PostMapping("/insertVideo")
    public R insertVideo(@RequestBody EduVideo eduVideo){
        System.out.println("读取到的eduVideo信息："+eduVideo);
        boolean flag = eduVideoService.save(eduVideo);
        if(flag){
            return R.ok();
        }
        return R.error();
    }

//    TODD 后面这个方法需要完善，删除小节的时候，同时也需要把视频删除
    @ApiOperation(value = "删除小节")
    @DeleteMapping("/{id}")
    /*删除小节*/
    public R deleteVideo(@PathVariable String id){
        System.out.println("33333"+id);
        boolean flag=eduVideoService.delete(id);
        if(flag){
            return R.ok();
        }
        return R.error();
    }

    /*修改小节*/
    @ApiOperation(value = "修改小节")
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        boolean flag = eduVideoService.updateById(eduVideo);
        if(flag==true){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value="根据小节的id查询小节里面的视频SourceId")
    @GetMapping("getVideo/{videoId}")
    public R getVideo(@PathVariable(value = "videoId") String videoId){
        /*前端传递过来的小节id,根据小节id查询视频的sourceId*/
        return eduVideoService.getVideo(videoId);
    }



    /*测试---------------------------------------------------*/



    @ApiOperation(value = "根据视频id删除视频")
    @GetMapping("/testDelete/{videoId}")
    public R testDelete(@PathVariable String videoId){
        R r = vodClient.removeAliVideo(videoId);
        return r;
    }

    @ApiOperation(value = "根据视频id集合删除视频")
    @GetMapping("/testDeletes")
    public R testDeletes(@RequestParam(value = "videoIds") ArrayList<String> videoIds){
        return vodClient.removeAliVideos(videoIds);
    }


    @ApiOperation(value = "根据视频id获取视频的播放权限")
    @GetMapping("/testAuth/{sourceId}")
    public R testAuth(@PathVariable(value = "sourceId") String sourceId){
        return vodClient.getPlayAuth(sourceId);
    }

}

